(function () {
    "use strict"; //JavaScript代码标志为“严格模式
    var productPageController=function ($scope, $http,$location,$modal,productService,toolsService,sambaService) {
       $scope.isProductsList = true;
       $scope.productList = JSON.parse(localStorage.getItem('bdfProductList'));

       //显示产品列表信息
       $scope.list = JSON.parse(localStorage.getItem('bdfProductList'));
       $scope.setVersionInfo = function (){
           var index = 0;
           angular.forEach($scope.list,function (item) {
               var versionList = item.product_version.split("/");
               var length = versionList.length;
               $scope.list[index].version = {};
               $scope.list[index].version = versionList[length-1];
               index++;
           });
       };
       $scope.setVersionInfo();

       //显示产品列表
       $scope.showProList = function(){
           $scope.isProductsList = true;
       }

       //显示选中产品信息表格
       $scope.tableData = {
           data :[]
       };
       $scope.init = false;
       $scope.tableInit = function () {
           var oTableInit = new Object();
           oTableInit.Init = function () {
               $('#productInfo').bootstrapTable({
                   data: $scope.tableData.data,
                   toolbar: '#toolbar',
                   dataField:"data",
                   striped: true,
                   cache: false,
                   pagination: true,
                   sidePagination:'client',
                   pageNumber: 1,
                   pageSize: 10,
                   search: true,
                   uniqueId: true,
                   cardView: false,
                   detailView: false,
                   responseHandler:responseHandler,
                   columns: [
                       {
                           field: 'productVersion',
                           title: '版本号',
                           sortable: true
                       }, {
                           field: 'fileName',
                           title: '文件名称',
                           sortable: true
                       }, {
                           field: 'date',
                           title: '更新日期',
                           sortable: true
                       }, {
                           field: 'size',
                           title: '数据大小',
                           formatter:sizeFormatter,
                           sortable: true
                       }, {
                           field: 'type',
                           title: '数据类型',
                           sortable: true
                       } ]
               });
           };
           return oTableInit;
       };

       function responseHandler(res) {
           $.each(res.data, function (i, row) {
               row.download = "下载";
           });
           return res;
       }

       function operateFormatter() {
           return [
               '<a class="download" href="javascript:void(0)" title="Download">',
               '<i class="Hui-iconfont Hui-iconfont-down"></i>',
               '</a>'
           ].join('');
       }
       function sizeFormatter(value, row, index) {
           var size="";
           if(value > 0 && value < 1000){
               size = value +"B";
           } else if(value >=1000 && value < 1000 * 1024) {
               size = (value / 1000).toFixed(2) + "KB";
           } else if(value >= 1000 * 1024 && value < 1000*1024*1024){
               size = (value / (1000 * 1024)).toFixed(2) + "MB";
           }  else {
               size = (value / (1000 * 1024 * 1024)).toFixed(2) + "GB";
           }
           return size;
       }

       //获取某个产品的具体版本信息
       $scope.productGet = function (id) {
           console.log("选中的产品ID:"+id);
           if($scope.productList != null && $scope.productList.length > 0){
               angular.forEach($scope.productList,function (item) {
                   if(id === item.id ){
                       $scope.product = item;
                       if(item.tarFileList != undefined){
                           $scope.tableData.data = item.tarFileList;
                           console.log("表"+$scope.tableData.data.length);
                           if(!$scope.init){
                               console.log("加载表格");
                               var oTable = $scope.tableInit();
                               oTable.Init();
                               $scope.init = true;
                           }else{
                               console.log("刷新表格");
                               $('#productInfo').bootstrapTable('load', $scope.tableData.data);
                           }
                       }
                   }

               });
           }

           if($scope.product.watchedBies != undefined && $scope.product.watchedBies.length > 0){
               angular.forEach($scope.product.watchedBies,function (item) {
                   if(item.role == 'FORMAL_DEV'){
                       $scope.product.fd = {
                           name:item.picName,
                           phone:item.picPhone
                       }
                   } else if(item.role == 'ASSISTANT_DEV'){
                       $scope.product.ad = {
                           name:item.picName,
                           phone:item.picPhone
                       }
                   } else if(item.role == 'FORMAL_TEST'){
                       $scope.product.ft = {
                           name:item.picName,
                           phone:item.picPhone
                       }
                   } else if(item.role == 'ASSISTANT_TEST'){
                       $scope.product.at = {
                           name:item.picName,
                           phone:item.picPhone
                       }
                   }
               });
           }
       };

       var type = toolsService.parameter("type",$location.absUrl());
       var id = toolsService.parameter("id",$location.absUrl());
       if(type != null){
           window.myiframe.location="product_etl_info.html?id="+id;
           productService.productGet({id:id}).success(function (data) {
               if(data.errorMsg != null){
                   $.Huimodalalert(data.errorMsg,1000);
               } else {
                   $scope.product = data.product;
                   if($scope.product.watchedBies != null && $scope.product.watchedBies.length > 0){
                       angular.forEach($scope.product.watchedBies,function (item) {
                           if(item.role == 'FORMAL_DEV'){
                               $scope.product.fd = {
                                   name:item.picName,
                                   phone:item.picPhone
                               }
                           } else if(item.role == 'ASSISTANT_DEV'){
                               $scope.product.ad = {
                                   name:item.picName,
                                   phone:item.picPhone
                               }
                           } else if(item.role == 'FORMAL_TEST'){
                               $scope.product.ft = {
                                   name:item.picName,
                                   phone:item.picPhone
                               }
                           } else if(item.role == 'ASSISTANT_TEST'){
                               $scope.product.at = {
                                   name:item.picName,
                                   phone:item.picPhone
                               }
                           }
                       });
                   }

                   if(type == 'add'){
                       $scope.doCreate();
                   } else {
                       var sambaId = toolsService.parameter("sambaId",$location.absUrl());
                       sambaService.sambaGet({id:sambaId}).success(function (data) {
                           if(data.errorMsg != null){
                               $.Huimodalalert(data.errorMsg,1000);
                           } else {
                               $scope.doUpdate(data.sambaExtend);
                           }
                       });
                   }
               }
           });
       }

       $scope.changeUrl = function (item,index) {
           if(item != null && item != undefined && item.id != null){
               $scope.isProductsList = false;
               $scope.productGet(item.id);
           } else {
               $scope.isProductsList = true;
           }
           $(".pointer").removeClass("left-hover").eq(index).addClass("left-hover");
       };






    };
    var productUseController=function ($scope, $http,$location,productService,toolsService) {
        $scope.list = [];
        $scope.vm = {
            pageSize:10,
            pageNumber:0
        };
        productService.productFindDetail($scope.vm).success(function (data) {
            if(data.errorMsg != null){
                $.Huimodalalert(data.errorMsg,1000);
            } else {
                $scope.list = data.result;
                var index = 0;
                angular.forEach($scope.list,function (item) {
                    var versionList = item.product_version.split("/");
                    var length = versionList.length;
                    $scope.list[index].version = {};
                    $scope.list[index].version = versionList[length-2];
                    index++;
                });
            }
        });
    };
    var productInfoController=function ($scope, $http,$location,$modal,productService,downloadService,toolsService) {
        $scope.tableData = {
            data :[]
        };
        $scope.init = false;
        $scope.canDownload = false;
        $scope.loading = false;
        $scope.downloadPath = "";
        $scope.vm = {
            id : toolsService.parameter("id", $location.absUrl()),
            pageSize:10,
            pageNumber:1
        };
        $scope.productGet = function () {
            productService.productGet($scope.vm).success(function (data) {
                if(data.errorMsg != null){
                    $.Huimodalalert(data.errorMsg,1000);
                } else {
                    $scope.product = data.product;
                    if($scope.product.watchedBies != null && $scope.product.watchedBies.length > 0){
                        angular.forEach($scope.product.watchedBies,function (item) {
                            if(item.role == 'FORMAL_DEV'){
                                $scope.product.fd = {
                                    name:item.picName,
                                    phone:item.picPhone
                                }
                            } else if(item.role == 'ASSISTANT_DEV'){
                                $scope.product.ad = {
                                    name:item.picName,
                                    phone:item.picPhone
                                }
                            } else if(item.role == 'FORMAL_TEST'){
                                $scope.product.ft = {
                                    name:item.picName,
                                    phone:item.picPhone
                                }
                            } else if(item.role == 'ASSISTANT_TEST'){
                                $scope.product.at = {
                                    name:item.picName,
                                    phone:item.picPhone
                                }
                            }
                        });
                    }
                    $scope.vm.paramJson = {
                        file_name:$scope.product.product_name
                    };
                    $scope.vm.type = 2;
                    var param = angular.copy($scope.vm);
                    param.pageSize = 0;
                    downloadService.fileList(param).success(function (data) {
                        if(data.errorMsg != null){
                            $.Huimodalalert(data.errorMsg,1000);
                        } else {
                            $scope.tableData.data = data.list;
                            angular.forEach($scope.tableData.data,function (item) {
                                if(item.date != null ){
                                    var date = new Date(item.date);
                                    item.date = date.format("yyyy-MM-dd hh:mm:ss");
                                }
                            });

                            if(!$scope.init){
                                var oTable = new TableInit();
                                oTable.Init();
                                $scope.init = true;
                            }
                        }
                    });
                }
            });
        };
        $scope.productGet();
        $scope.download = function (product) {
            $scope.loading = true;
            product.id = $scope.vm.id;
            downloadService.fileDownloadDir(product).success(function (data) {
                $scope.loading = false;
                if(data.errorMsg != null){
                    alert(data.errorMsg);
                } else {
                    var fileName = data.fileName;
                    console.log(fileName);
                    //特殊字符+，&转义处理
                    fileName = fileName.replace(/\+/g, "%2B");
                    fileName = fileName.replace(/\&/g, "%26");
                    window.location.href = baseUrl+"file/download?type=1&fileName=" + fileName;
                }
            });
        };

        var TableInit = function () {
            var oTableInit = new Object();
            oTableInit.Init = function () {
                $('#etlInfo').bootstrapTable({
                    data:$scope.tableData.data,
                    toolbar: '#toolbar',
                    dataField:"data",
                    striped: true,
                    cache: false,
                    pagination: true,
                    sidePagination:'client',
                    pageNumber: 1,
                    pageSize: 10,
                    search: true,
                    uniqueId: true,
                    cardView: false,
                    detailView: false,
                    // detailFormatter:"detailFormatter",
                    responseHandler:responseHandler,
                    columns: [
                        {
                            field: 'productVersion',
                            title: '版本号',
                            sortable: true
                        }, {
                            field: 'file_name',
                            title: '文件名称',
                            sortable: true
                        }, {
                            field: 'date',
                            title: '更新日期',
                            sortable: true
                        }, {
                            field: 'size',
                            title: '数据大小',
                            formatter:sizeFormatter,
                            sortable: true
                        }, {
                            field: 'type',
                            title: '数据类型',
                            sortable: true
                        }, {
                            field: 'download',
                            title: '下载操作',
                            align: 'center',
                            events: operateEvents,
                            formatter: operateFormatter
                        } ]
                });
            };
            return oTableInit;
        };
        function responseHandler(res) {
            $.each(res.data, function (i, row) {
                row.download = "下载";
            });
            return res;
        }
/*        function detailFormatter(index, row) {
            var html = [];
            var json = row.projectList;
            if(json != null && json.length > 0){
                html.push('<p><b>历史版本记录 :</p>');
                $.each(row.projectList, function (title, value) {
                    $.each(value,function (index,value) {
                        if(index == 'projectName'){
                            index = '项目名称';
                        } else if(index == 'projectVersion'){
                            index = '项目版本号';
                        }
                        html.push('<p><b>' + index + ':<b/> ' + value + '</p>');
                    });
                });
            }
            return html.join('');
        }*/
        function operateFormatter() {
            return [
                '<a class="download" href="javascript:void(0)" title="Download">',
                '<i class="Hui-iconfont Hui-iconfont-down"></i>',
                '</a>'
                // ,
                // '<a class="edit " style="margin-left: 16px;" href="javascript:void(0)" title="Edit">',
                // '<i class="Hui-iconfont Hui-iconfont-edit"></i>',
                // '</a>'
            ].join('');
        }
        function sizeFormatter(value, row, index) {
            var size="";
            if(value > 0 && value < 1000){
                size = value +"B";
            } else if(value >=1000 && value < 1000 * 1024) {
                size = (value / 1000).toFixed(2) + "KB";
            } else if(value >= 1000 * 1024 && value < 1000*1024*1024){
                size = (value / (1000 * 1024)).toFixed(2) + "MB";
            }  else {
                size = (value / (1000 * 1024 * 1024)).toFixed(2) + "GB";
            }
            return size;
        }
        window.operateEvents = {
            'click .download': function (e, value, row, index) {
                $scope.download({version:row.productVersion,type:2});
            },
            'click .edit': function (e, value, row, index) {
                $scope.doUpdate(row);
            }
        };

        $scope.doUpdate = function (row) {
            //window.parent.location.href="productpage.html?type=update&id="+$scope.vm.id+"&sambaId="+row.id;
            //弹出窗口
            var modalInstance = $modal.open({
                templateUrl: "productVersion.html",
                controller:productVersionController,
                size:"lg",
                resolve: {
                    items: function () {
                        return {
                            type:'update',
                            product:$scope.product,
                            sambaExtend:row
                        };
                    }
                }
            });
            modalInstance.result.then(function (data) {
                $scope.productGet();
            });
        };
        $scope.doCreate = function () {
            //window.parent.location.href="productpage.html?type=add&id="+$scope.vm.id;
            //弹出窗口
            var modalInstance = $modal.open({
                templateUrl: "productVersion.html",
                controller:productVersionController,
                size:"lg",
                resolve: {
                    items: function () {
                        return {
                            type:'create',
                            product:$scope.product
                        };
                    }
                }
            });
            modalInstance.result.then(function (data) {
                $scope.productGet();
            });
        };
    };

    var productVersionController=["$scope","$modal", "$modalInstance","items","DownloadService",
        function ($scope, $modal,$modalInstance,items,downloadService) {

        $scope.loading = false;
        var sambaList = [];
        $scope.product = items.product;
        $scope.type = items.type;
        if($scope.type == 'update'){
            $scope.data = items.sambaExtend;
            $scope.version = $scope.data.productVersion;
            $scope.fileName = $scope.data.file_name;
            $scope.sambaId = $scope.data.id;
        }

        $scope.doCancel = function () {
            $modalInstance.dismiss('cancel');
        };

        //数据提交
        $scope.doUpload = function () {
            $scope.loading = true;
            var fd = new FormData();
            if($scope.version == null || $scope.version == undefined || $.trim($scope.version) == ''){
                $scope.loading = false;
                $.Huimodalalert("请输入版本号",1000);
                return;
            }
            var files = document.querySelector('input[type=file]').files;
            if(files.length == 0){
                $scope.loading = false;
                $.Huimodalalert("请选择要上传的文件",1000);
                return;
            }
            angular.forEach(files,function (item) {
                fd.append('file[]', item);
            });
            fd.append('name',$scope.product.product_name);
            fd.append('id',$scope.product.id);
            fd.append('version',$scope.version);
            fd.append('type',$scope.type);
            angular.forEach(sambaList,function (item) {
                fd.append('samba[]', JSON.stringify(item));
            });
            downloadService.fileUpload(fd).success(function (data) {
                if(data.errorMsg != null){
                    $scope.loading = false;
                    $.Huimodalalert(data.errorMsg,2000);
                } else {
                    $scope.loading = false;
                    $.Huimodalalert("上传成功！",1500);
                    window.location.href="product_etl_info.html?id="+$scope.product.id;
                }
            });
        };
    }];

    var productTestRecordController=function ($scope, $http,$location,productService,toolsService) {
        $scope.productList = [];
        $scope.init = false;
        $scope.vm = {
            id : toolsService.parameter("id", $location.absUrl()),
            //id:1,
            pageSize:20,
            pageNumber:1
        };
        $scope.hasError = false;
        $scope.errorMsgs = [];
        $scope.getProductTestRecord = function () {
            productService.productGet($scope.vm).success(function (data) {
                if(data.errorMsg != null){
                    $.Huimodalalert(data.errorMsg,1000);
                } else {
                    $scope.product = data.product;
                    if($scope.product.productLatestUpdateTime != null){
                        var date = new Date($scope.product.productLatestUpdateTime);
                        $scope.product.productLatestUpdateTime = date.format("yyyy-MM-dd hh:mm:ss");
                    }
                    if($scope.product.productLastestDownloadedTime != null){
                        var date = new Date($scope.product.productLastestDownloadedTime);
                        $scope.product.productLastestDownloadedTime = date.format("yyyy-MM-dd hh:mm:ss");
                    }
                    if($scope.product.watchedBies != null && $scope.product.watchedBies.length > 0){
                        angular.forEach($scope.product.watchedBies,function (item) {
                            if(item.role == 'FORMAL_DEV'){
                                $scope.product.fd = {
                                    name:item.picName,
                                    phone:item.picPhone
                                }
                            } else if(item.role == 'ASSISTANT_DEV'){
                                $scope.product.ad = {
                                    name:item.picName,
                                    phone:item.picPhone
                                }
                            } else if(item.role == 'FORMAL_TEST'){
                                $scope.product.ft = {
                                    name:item.picName,
                                    phone:item.picPhone
                                }
                            } else if(item.role == 'ASSISTANT_TEST'){
                                $scope.product.at = {
                                    name:item.picName,
                                    phone:item.picPhone
                                }
                            }
                        });
                    }
                    productService.productTestRecordFind({productId:$scope.product.id}).success(function (data) {
                        if(data.errorMsg != null){
                            $.Huimodalalert(data.errorMsg,1000);
                        } else {
                            $scope.tableData = data.result;
                            angular.forEach($scope.tableData,function (row) {
                                if(row.test_call_time != null){
                                    var date = new Date(row.test_call_time);
                                    row.test_call_time = date.format("yyyy-MM-dd");
                                }
                                if(row.test_report_publish_time != null){
                                    var date = new Date(row.test_report_publish_time);
                                    row.test_report_publish_time = date.format("yyyy-MM-dd");
                                }
                            });
                            if(!$scope.init){
                                var oTable = new TableInit();
                                oTable.Init();
                                $scope.init = true;
                            }
                        }
                    });
                }
            });
        };
        $scope.getProductTestRecord();
        var $table = $('#etlVt'),
            $remove = $('#remove'),
            $add = $('#add'),
            $save = $('#save'),
            $refresh = $('#refresh'),
            selections = [];
        var TableInit = function () {
            var oTableInit = new Object();
            oTableInit.Init = function () {
                $table.bootstrapTable({
                    data:$scope.tableData,
                    toolbar: '#toolbar',
                    striped: true,
                    cache: false,
                    showRefresh :false,
                    pagination: true,
                    sortable: true,
                    sortOrder: 'asc',
                    search:true,
                    searchAlign:'left',
                    sidePagination: "client",
                    queryParams: oTableInit.queryParams,
                    pageNumber: 1,
                    pageSize: 10,
                    pageList: [10, 25, 50, 100],
                    clickToSelect: false,
                    uniqueId: true,
                    cardView: false,
                    detailView: false,
                    rowStyle:rowStyle,
                    rowAttributes:rowAttributes,
                    cellStyle:cellStyle,
                    responseHandler : responseHandler,
                    columns:[
                        [  {
                            field: 'state',
                            checkbox: true,
                            rowspan: 2,
                            align: 'center',
                            valign: 'middle'
                        },{
                            field: 'test_product_name',
                            title: '产品名称',
                            editable: true,
                            align: 'center',
                            valign: 'middle',
                            rowspan: 2
                        }, {
                            field: 'test_product_version',
                            title: '提测版本',
                            align: 'center',
                            valign: 'middle',
                            editable: true,
                            rowspan: 2
                        }, {
                            field: 'test_dev_caller',
                            title: '提测人',
                            editable: true,
                            align: 'center',
                            valign: 'middle',
                            rowspan: 2
                        }, {
                            field: 'test_call_time',
                            title: '提测时间',
                            editable: {
                                type: 'date'
                            },
                            //editable: true,
                            align: 'center',
                            valign: 'middle',
                            rowspan: 2
                        }, {
                            field: 'test_branch',
                            title: '提测分支',
                            editable: true,
                            align: 'center',
                            valign: 'middle',
                            rowspan: 2
                        } , {
                            field: 'test_executor_name',
                            title: '测试人员',
                            editable: true,
                            align: 'center',
                            valign: 'middle',
                            rowspan: 2
                        } , {
                            field: 'test_report_publish_time',
                            title: '报告时间',
                            editable: {
                                type: 'date'
                            },
                            // editable: true,
                            align: 'center',
                            valign: 'middle',
                            rowspan: 2
                        } , {
                            field: 'smoke_pass_rate',
                            title: '冒烟测试通过率',
                            editable: true,
                            align: 'center',
                            valign: 'middle',
                            rowspan: 2
                        } , {
                            field: 'return_times',
                            title: '退回次数',
                            editable: true,
                            align: 'center',
                            valign: 'middle',
                            rowspan: 2
                        } , {
                            field: 'test_product_config_info',
                            title: '配置信息',
                            editable: true,
                            align: 'center',
                            valign: 'middle',
                            rowspan: 2
                        }, {
                            title: 'Test',
                            align: 'center',
                            valign: 'middle',
                            colspan: 2
                        },  {
                            title: 'Stable',
                            align: 'center',
                            valign: 'middle',
                            colspan: 3
                        }],
                        [{
                            field: 'test_product_test_dir_packages',
                            align: 'center',
                            editable: {
                                type: 'select',
                                title: '部署包',
                                source:[{value:"1",text:"是"},{value:"0",text:"否"}]
                            },
                            valign: 'middle',
                            title: '部署包'
                        },{
                            field: 'test_product_test_dir_license',
                            align: 'center',
                            editable: {
                                type: 'select',
                                title: 'license',
                                source:[{value:"1",text:"是"},{value:"0",text:"否"}]
                            },
                            valign: 'middle',
                            title: 'license'
                        },{
                            field: 'test_product_stable_dir_packages',
                            align: 'center',
                            editable: {
                                type: 'select',
                                title: '部署包',
                                source:[{value:"1",text:"是"},{value:"0",text:"否"}]
                            },
                            valign: 'middle',
                            title: '部署包'
                        },{
                            field: 'test_product_stable_dir_license',
                            align: 'center',
                            editable: {
                                type: 'select',
                                title: 'license',
                                source:[{value:"1",text:"是"},{value:"0",text:"否"}]
                            },
                            valign: 'middle',
                            title: 'license'
                        },{
                            field: 'test_product_docs',
                            align: 'center',
                            editable: {
                                type: 'select',
                                title: '安装部署手册',
                                source:[{value:"1",text:"是"},{value:"0",text:"否"}]
                            },
                            valign: 'middle',
                            title: '安装部署手册'
                        }]
                    ]
                });
            };
            return oTableInit;
        };
        function getIdSelections() {
            return $.map($table.bootstrapTable('getSelections'), function (row) {
                return row
            });
        }
        function cellStyle(value, row, index, field) {
            return {
                classes: 'text-nowrap another-class',
                css: {"color": "blue", "font-size": "50px"}
            };
        }
        function responseHandler(res) {
            $.each(res.data, function (i, row) {
                row.state = $.inArray(row.id, selections) !== -1;
            });
            return res;
        }
        function rowStyle(row, index) {
            if(index % 2 == 0){
                return {
                    //classes: ''
                    classes: 'text-nowrap another-class',
                    css: {"border-top":"0px solid #ddd"}
                };
            } else {
                return {
                    //classes: 'info'
                    classes: 'text-nowrap another-class',
                    css: {"border-top":"0px solid #ddd"}
                }
            }
        }
        function rowAttributes(row, index) {
            if(index % 2 == 0){
                return {
                    'style': 'color:#6B6B6B;font-size:13px;background-color:white'
                };
            } else {
                return {
                    'style': 'color:#6B6B6B;font-size:13px;background-color:#F7FCFE'
                }
            }
        }

        $table.on('check.bs.table uncheck.bs.table ' +
            'check-all.bs.table uncheck-all.bs.table', function () {
            $remove.prop('disabled', !$table.bootstrapTable('getSelections').length);
            // save your data, here just save the current page
            selections = getIdSelections();
            // push or splice the selections if you want to save all data selections
        });
        $remove.click(function () {
            var rows = getIdSelections();
            var list = [];
            var deleteList = [];
            angular.forEach(rows,function (row) {
                if(row.id == null){
                    list.push(true);
                } else {
                    var item = {id:row.id};
                    deleteList.push(item);
                }
            });
            if(list.length > 0){
                $table.bootstrapTable('remove', {field: 'state', values: list});
                $.Huimodalalert("删除成功",1000);
            }
            if(deleteList.length > 0){
                productService.productTestRecordDelete(deleteList).success(function (data) {
                    if(data.errorMsg != null){
                        $.Huimodalalert(data.errorMsg,1000);
                    } else {
                        $.Huimodalalert("删除成功",1000);
                        $table.bootstrapTable('destroy');
                        $scope.init = false;
                        $scope.getProductTestRecord();
                    }
                });
            }
        });
        $refresh.click(function () {
            $table.bootstrapTable('destroy');
            $scope.init = false;
            $scope.getProductTestRecord();
        });
        // yyyy-mm-dd hh:mm:ss格式的正则表达式
        // var timePatten = /^((((1[6-9]|[2-9]\d)\d{2})-(0?[13578]|1[02])-(0?[1-9]|[12]\d|3[01]))|(((1[6-9]|[2-9]\d)\d{2})-(0?[13456789]|1[012])-(0?[1-9]|[12]\d|30))|(((1[6-9]|[2-9]\d)\d{2})-0?2-(0?[1-9]|1\d|2[0-8]))|(((1[6-9]|[2-9]\d)(0[48]|[2468][048]|[13579][26])|((16|[2468][048]|[3579][26])00))-0?2-29-)) (20|21|22|23|[0-1]?\d):[0-5]?\d:[0-5]?\d$/;

        $save.click(function () {
            $scope.hasError = false;
            $scope.errorMsgs = [];
            var tableData  = $table.bootstrapTable('getData');
            for(var i=0;i<tableData.length;i++){
                var j = i+1;
                var row = tableData[i];
                row.product = {
                    id:$scope.product.id
                };
                /*if(row.testCallTime != null && $.trim(row.testCallTime) != ""){
                    if(!timePatten.test(row.testCallTime)){
                        $scope.errorMsgs.push("第"+j+"条数据，提测时间格式不对,请输入yyyy-mm-dd hh:mm:ss格式数据");
                        $scope.hasError = true;
                    }
                }
                if(row.testReportPublishTime != null && $.trim(row.testReportPublishTime) != ""){
                    if(!timePatten.test(row.testReportPublishTime)){
                        $scope.errorMsgs.push("第"+j+"条数据，报告时间格式不对,请输入yyyy-mm-dd hh:mm:ss格式数据");
                        $scope.hasError = true;
                    }
                }*/
                if(row.smokePassRate != null && row.smokePassRate != undefined && $.trim(row.smokePassRate) != ""){
                    var spr = Number(row.smokePassRate);
                    if(isNaN(spr) || !angular.isNumber(spr)){
                        $scope.errorMsgs.push("第"+j+"条数据，冒烟测试通过率必须为数字");
                        $scope.hasError = true;
                    } else {
                        if(spr > 100 || spr < 0){
                            $scope.errorMsgs.push("第"+j+"条数据，冒烟测试通过率必须为在0~100之间");
                            $scope.hasError = true;
                        }
                    }
                }

                if(row.returnTimes != null && row.returnTimes != undefined && $.trim(row.returnTimes) != ""){
                    var rt = Number(row.returnTimes);
                    if(isNaN(rt) || !angular.isNumber(rt)){
                        $scope.errorMsgs.push("第"+j+"条数据，退回次数必须为数字");
                        $scope.hasError = true;
                    } else {
                        if(rt < 0){
                            $scope.errorMsgs.push("第"+j+"条数据，退回次数必须大于0");
                            $scope.hasError = true;
                        }
                    }
                }
            }
            if($scope.hasError){
                if ($scope.errorMsgs.length > 5){
                    $scope.errorMsgs = $scope.errorMsgs.splice(0,5);
                }
                $scope.$apply();
            }
            if(!$scope.hasError){
                productService.productTestRecordSave(tableData).success(function (data) {
                    if(data.errorMsg != null){
                        //$.Huimodalalert(data.errorMsg,1000);
                        alert(data.errorMsg);
                    } else {
                        $.Huimodalalert("保存成功",1000);
                        $table.bootstrapTable('destroy');
                        $scope.init = false;
                        $scope.getProductTestRecord();
                    }
                });
            }
        });
        $add.click(function () {
            var data = initNewRow();
            $table.bootstrapTable('insertRow', {index: 0, row: data});
        });
        function initNewRow() {
            var pn = "";
            if($scope.product.product_name != null){
                pn = $scope.product.product_name;
            }
            return {
                state:false,
                test_product_name:pn,
                test_product_version:"",
                test_dev_caller:"",
                test_call_time:null,
                test_branch:"",
                test_executor_name:"",
                test_report_publish_time:null,
                smoke_pass_rate:"",
                return_times:"",
                test_product_config_info:"",
                test_product_test_dir_packages:"0",
                test_product_test_dir_license:"0",
                test_product_stable_dir_packages:"0",
                test_product_stable_dir_license:"0",
                test_product_docs:"0"
            };
        }
    };

    angular.module("cmss.product",[])
        .controller("ProductPageController",["$scope","$http","$location","$modal","ProductService","toolsService","SambaService",productPageController])
        .controller("ProductUseController",["$scope","$http","$location","ProductService","toolsService",productUseController])
        .controller("ProductTestRecordController",["$scope","$http","$location","ProductService","toolsService",productTestRecordController])
        .controller("ProductInfoController",["$scope","$http","$location","$modal","ProductService","DownloadService","toolsService",productInfoController])
        .controller("ProductVersionController",["$scope","$http","$location","$modal","ProductService","DownloadService","SambaService","toolsService",productVersionController])
})();
